var g_object_name='';
function upload_apk(config,success_func){
    var uploader = new plupload.Uploader({
        runtimes : 'html5,flash,silverlight,html4',
        browse_button : config.browse_button,
        multi_selection: false,
        //container: config.container,
        flash_swf_url : '/js/plugins/plupload-2.1.2/Moxie.swf',
        silverlight_xap_url : '/js/plugins/plupload-2.1.2/Moxie.xap',
        url : 'http://oss.aliyuncs.com',
        filters: {
            mime_types : [ //只允许上传图片和zip,rar文件
                { title : "Image files", extensions : "jpg,gif,png,jpeg,apk" },
                { title : "Rar files", extensions : "rar" },
                { title : "Zip files", extensions : "zip" },
            ],
            max_file_size : '100mb', //最大只能上传10mb的文件
            prevent_duplicates : false //false:允许选取重复文件
        },
        init: {
            PostInit: function() {
                set_upload_param(uploader, '', false);
            },
            FilesAdded: function(up, files) {
                plupload.each(files, function(file) {
                    set_upload_param(up, file.name, true);
                });
            },
            BeforeUpload: function(up, file) {},
            UploadProgress: function(up, file) {
                $('#'+config.browse_button).siblings('.upload_progress').show().find('.progress-bar').css({'width':file.percent+'%'});
            },

            FileUploaded: function(up, file, info) {
                if (info.status == 200)
                {
                    $('#'+config.browse_button).siblings('.upload_progress').hide().find('.progress-bar').css({'width':'0%'});
                    success_func(up, file, info);
                }else{
                    alert(info.response);
                }
            },

            Error: function(up, err) {
                if (err.code == -600) {
                    alert("选择的文件太大了,可以根据应用情况，在upload.js 设置一下上传的最大大小");
                }
                else if (err.code == -601) {
                    alert("选择的文件后缀不对,可以根据应用情况，在upload.js进行设置可允许的上传文件类型");
                }
                else if (err.code == -602) {
                    alert("这个文件已经上传过一遍了");
                }else{
                    alert("Error xml:" + err.response);
                }
            }
        }
    });
    uploader.init();
}



function get_signature()
{
    host = ossUpload['host']
    policyBase64 = ossUpload['policy']
    accessid = ossUpload['accessid']
    signature = ossUpload['signature']
    expire = parseInt(ossUpload['expire'])
    callbackbody = ossUpload['callback']
    key = ossUpload['dir']
    return true;
};

function random_string(len) {
    len = len || 32;
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var maxPos = chars.length;
    var pwd = '';
    for (i = 0; i < len; i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

function get_suffix(filename) {
    pos = filename.lastIndexOf('.')
    suffix = ''
    if (pos != -1) {
        suffix = filename.substring(pos)
    }
    return suffix;
}


function set_upload_param(up, filename, ret)
{
    if (ret == false)
    {
        ret = get_signature();
        g_object_name = key;
        if (filename != '') {
            suffix = get_suffix(filename);
            g_object_name = key + random_string(10) + suffix;
        }
    }else {
        g_object_name = key + filename;
    }
    new_multipart_params = {
        'key' : g_object_name,
        'policy': policyBase64,
        'OSSAccessKeyId': accessid,
        'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
        'callback' : callbackbody,
        'signature': signature,
    };

    up.setOption({
        'url': host,
        'multipart_params': new_multipart_params
    });

    up.start();
}